# -*- coding: utf-8 -*-    
from django.core.urlresolvers import reverse
from django.template import Library
from django.utils.safestring import mark_safe
import math
import datetime

register = Library()

MENU = [
    (u'Все задачи', reverse('task_list')),
    (u'Проекты', reverse('project_list')),
]

@register.inclusion_tag('templatetags/top_menu.html')
def top_menu():
    return {}

@register.inclusion_tag('templatetags/task_card.html')
def task_card(task):
    return {
        'task': task,
    }
    
@register.inclusion_tag('templatetags/form.html')
def show_form(form):
    return {'form': form }

of_months = [
    '',
    u'января',u'февраля',u'марта',u'апреля',u'мая',u'июня',u'июля',u'августа',
    u'сентября',u'октября',u'ноября',u'декабря'
]

@register.filter
def format_datetime(dt):
    if not dt:
        return dt

    now = datetime.datetime.now()

    if (now.day, now.month, now.year) == (dt.day, dt.month, dt.year):
        #mask = u'сегодня, %(hour)02d:%(minute)02d:%(second)02d'
        mask = u'сегодня, %(hour)02d:%(minute)02d'
    elif (now.month, now.year) == (dt.month, dt.year):
        #mask = u'%(day)d&nbsp;%(of_month)s, %(hour)02d:%(minute)02d'
        mask = u'%(day)d&nbsp;%(of_month)s, %(hour)02d'
    elif now.year == dt.year:
        mask = u'%(day)d&nbsp;%(of_month)s'
    else:
        mask = u'%(day)d&nbsp;%(of_month)s %(year)s&nbsp;г.'

    return mark_safe(mask % {
        'year': dt.year,
        'of_month': of_months[dt.month],
        'day': dt.day,
        'hour': dt.hour,
        'minute': dt.minute,
        'second': dt.second,
    })

def numeral_suffix(num, low, mid, hi):
    last_digit, last_two_digits = num % 10, num % 100

    if 4 < last_two_digits < 21:
        suf = hi
    elif last_digit == 1:
        suf = low
    elif 1 < last_digit < 5:
        suf = mid
    else:
        suf = hi
    return u'%d&nbsp;%s' % (num, suf)

MINUTE = 60
HOUR = 60 * MINUTE
DAY= 24 * HOUR
MONTH = 30 * DAY
YEAR = 365 * DAY

#def human_seconds(sec):
#    return numeral_suffix(sec % MINUTE, u'секунда', u'секунды', u'секунд')

def human_minutes(sec):
    return numeral_suffix(sec, u'минута', u'минуты', u'минут')

def human_hours(sec):
    return numeral_suffix(sec, u'час', u'часа', u'часов')

def human_days(sec):
    return numeral_suffix(sec, u'день', u'дня', u'дней')

def human_months(sec):
    return numeral_suffix(sec, u'месяц', u'месяца', u'месяцев')

def human_years(sec):
    return numeral_suffix(sec, u'год', u'года', u'лет')

def split_time(sec, u1, u2):
    t2 = math.floor(float(sec) / u2)
    t1 = (sec - t2 * u2) // u1
    return t1, t2

def compose(f2, f1, u2, u1):
    return u2 and mark_safe('%s %s' % (f1(u1), f2(u2))) or mark_safe(f1(u1))

@register.filter
def format_timedelta(td):
    sec = td.days * DAY + td.seconds

    if sec < HOUR:
        m = sec // MINUTE
        return mark_safe(human_minutes(m))

    if HOUR <= sec < DAY:
        return compose(human_minutes, human_hours, *split_time(sec, MINUTE, HOUR))

    if DAY <= sec < MONTH:
        return compose(human_hours, human_days, *split_time(sec, HOUR, DAY))

    if MONTH <= sec < YEAR:
        return compose(human_days, human_months, *split_time(sec, DAY, MONTH))

    return compose(human_months, human_years, *split_time(sec, MONTH, YEAR))

#@register.filter
#def startswith(s, path):
#    return s.startswith(path)
